package com.imay.live.activity.dynamics;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.imay.live.Constants;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.fragment.dynamics.LinearFragment;
import com.imay.live.model.FeedModel;
import com.imay.live.R;
import com.imay.tbas_api.IMYTBASObject;

import java.util.ArrayList;

/**
 * Created by chan on 2017/04/14 0014.
 * 动态列表
 */

public class LabelFeedsDetailActivity extends BaseRequestActivity {
    final static String TITLE = "title";
    public final static String POSITION = "position";
    public final static String FEEDSDATA = "feedmodelid";
    View progress, load_layout;
    TextView load_text;
    private Animation mRotateAnimation;
    private IMYTBASObject dynamicsupdate;

    public static void start(Activity context, String title, int position, ArrayList<FeedModel> feedsData) {
        Intent intent = new Intent(context, LabelFeedsDetailActivity.class);
        intent.putExtra(TITLE, title);
        intent.putExtra(POSITION, position);
        intent.putParcelableArrayListExtra(FEEDSDATA, feedsData);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dynamicactivity_layout;
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        com.imay.live.utils.ViewUtil.setForgroundRadius($(R.id.fragment_id));
        String title = getIntent().getStringExtra(TITLE);
        setTitle(title);
        initLoadLayout();
        completeLoad();
        getIntent().putExtra(LinearFragment.FOLLOW, true);
        LinearFragment linearFragment = LinearFragment.newInstance();
        addFragment(linearFragment);
    }


    void initLoadLayout() {
        progress = $(R.id.progress_icon);
        load_layout = $(R.id.load_layout);
        load_text = $(R.id.load_text);
        mRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.scan_rotate);
        progress.setAnimation(mRotateAnimation);
        load_layout.setVisibility(View.VISIBLE);
        progress.startAnimation(mRotateAnimation);
    }

    void completeLoad() {
        load_layout.setVisibility(View.GONE);
        progress.clearAnimation();
    }

    void loadError(String error) {
        progress.clearAnimation();
        load_text.setText(error);
    }

    private void addFragment(Fragment loadFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(this.getLocalClassName());
        if (fragment == null) {
            transaction.add(R.id.fragment_id, loadFragment, this.getLocalClassName());
        }
        transaction.show(loadFragment);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void finish() {
        setResult(RESULT_OK, getIntent());
        super.finish();
    }
}
